<?php
namespace app\app\controller;

use think\Controller;
class Policy extends Controller
{
	public function index()
    {
        $talk = new Talk();
        $if_login = $talk->check_login();
        if ($if_login == false){
            $this->assign('login_status','never');
        }else {
            $talk = new Talk();
            $never_read_num = $talk->never_read();
            $this->assign('never_read_num',$never_read_num);
            $this->assign('never_read_num',$never_read_num);
            $this->assign('user_name',session('user_name','','app'));
            $this->assign('user_id',session('user_id','','app'));
            $this->assign('login_status','have');
            $this->assign('logout_url',url('index.php/app/talk/logout'));
            $this->assign('personal_url',url('index.php/app/main/personal_set'));
        }
        $this->assign('active','policy');
        $this->assign('title','文件');
        return $this->fetch('index');
    }
    
    public function policy_list_view()
    {
    	$keyword = trim(input('get.keyword'));
    	$model = model('Policy');
    	if (session('user_id','','app')){
    		$policy_list = $model->getPolicyList($keyword,session('user_id','','app'),session('role','','app'));
    		$page = $policy_list->render();
    		$this->assign('page',$page);
    		$this->assign('policy_list',$policy_list);
    	}
    	$this->assign('title','文件列表');
    	return $this->fetch('policy_list');
    	
    }
    public function policy_content_view()
    {
    	$policy_id = trim(input('get.policy_id'));
    	$policy_info = db('policy')->where('id',$policy_id)->find();
    	$policy_info['attachments'] = json_decode($policy_info['attachments'],true);
    	$policy_info['createdwho'] = db('user')->where('id',$policy_info['createdwho'])->value('user_name');
    	$this->assign('policy',$policy_info);
    	$this->assign('title','文件内容');
    	return $this->fetch('policy_content');
    }
    
    public function download_attachment()
    {
    	$file_path = trim(input('file_path'));
    	if ($file_path == ''){
    		return false;
    	}
    	$file_path = ROOT_PATH.'Uploads'.DS.'policy'.DS.$file_path;
    	$this->download($file_path);
    }
    
    public  function download($file_url,$new_name=''){
    	$file_name = explode("\\",$file_url);
    	$file_name = end($file_name);
    	$file_url=iconv('utf-8','gb2312',$file_url);
    
    	//将编码转为支持中英文的gb2312编码
    	if(!isset($file_url)||trim($file_url)==''){
    		echo  '500';
    		die();
    	}
    	if(!file_exists($file_url)){ //检查文件是否存在
    		echo '404';
    		die();
    	}
    	$file_type=explode('.',$file_url);
    	$file_type=$file_type[count($file_type)-1];
    	$file_name=trim($new_name)==''?$file_name:urlencode($new_name).'.'.$file_type;
    
    	ob_clean();
    	//输入文件标签
    	header("Content-type: application/octet-stream");
    	header("Accept-Ranges: bytes");
    	header("Accept-Length: ".filesize($file_url));
    	header("Content-Disposition: attachment; filename=".$file_name);
    	 
    	$file_type=fopen($file_url,'r'); //打开文件
    	//输出文件内容
    	$file_size=filesize($file_url);//获取文件大小
    	$buffer=1024;   //定义1KB的缓存空间
    	$file_count=0;  //计数器,计算发送了多少数据
    	while(!feof($file_type) && ($file_size>$file_count)){
    		//如果文件还没读到结尾，且还有数据没有发送
    		$senddata=fread($file_type,$buffer);
    		//读取文件内容到缓存区
    		$file_count+=$senddata;
    		echo $senddata;
    	}
    
    	flush();
    
    	//         echo fread($file_type,filesize($file_url));
    	fclose($file_type);
    	exit();
    }
    
    
}